<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/10
 * Time: 11:58
 */

namespace Game\Module;


use Game\Lib\CodeConst;
use Game\Traits\Singleton;
use Game\Lib\OptConst;

class Tools{
    use Singleton;

    /**
     * 拆解接收的json字符串
     * @param string $jsonString json字符串
     * @return mixed
     */
    public function jsonDecode($jsonString){
        if(empty($jsonString)) {
            return OptConst::JSON_FALSE;
        }
        //判断是否为JSON格式
        if($return = json_decode($jsonString,true)) {
            return $return;
        } else {
            //不是json格式
            return OptConst::JSON_FALSE;
        }
    }


    /**
     * 组装返回值
     * @param $result
     * @param string $operation
     * @return mixed
     */
    public function formartReturn($result,$operation=''){
        //判断返回结果
        if(is_array($result)) {
            $resultArray = $result;
        } else {
            $resultArray = array('result'=>$result,'operation'=>$operation,'data'=>array(),'result_message'=>"");
        }
        return $this->jsonEncode($resultArray);		//编译为JSON格式,返回结果
    }

    /**
     * 数组转JSON格式
     * @param $array
     * @return string
     */
    public function jsonEncode($array){
//        var_dump($array);
        $this->__arrayRecursive($array, 'urlencode', true);
//        var_dump($array);
        $json = json_encode($array);
//        var_dump($array);
        return urldecode($json);
    }

    private function __arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            Logger::getInstance()->error( "function(checkRequestFormat):possible deep recursion attack"." in file".__FILE__." on Line ".__LINE__);
            return;
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $this->__arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else if(is_object($value)) {
                $array[$key] = $value;
            } else {
                $array[$key] = $function($value);
            }

            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }


    /**
     * 判断数据格式是否正确
     * @param $postArr
     * @return array|bool
     */
    public function checkRequestParams($postArr){
        if(!isset($postArr['operation'])) {
            Logger::getInstance()->error( "function(checkRequestParams):lack of operation"." in file ".__FILE__." on Line ".__LINE__);
            return array('result'=>CodeConst::Missing_Parameter,'operation'=>"",'data'=>array("missing_parameter"=>"operation"),'result_message'=>"缺少参数");
        }
        if($postArr['operation'] != "CreateRoom" && !isset($postArr['room_number'])){
            Logger::getInstance()->error( "function(checkRequestParams):lack of room_number"." in file ".__FILE__." on Line ".__LINE__);
            return array('result'=>CodeConst::Missing_Parameter,'operation'=>$postArr['operation'],'data'=>array("missing_parameter"=>"room_number"),'result_message'=>"缺少参数");
        }
        if(!isset($postArr['data'])) {
            Logger::getInstance()->error( "function(checkRequestParams):lack of data"." in file ".__FILE__." on Line ".__LINE__);
            return array('result'=>CodeConst::Missing_Parameter,'operation'=>$postArr['operation'],'data'=>array("missing_parameter"=>"data"),'result_message'=>"缺少参数");
        }
        return true;
    }

    /**
     * 检查格式
     * @param $jsonString
     * @return int
     */
    public function checkupRequestFormat($jsonString)
    {
        //解析json字符串为数组
        $jsonArray = $this->jsonDecode($jsonString);
        //判断是否为json格式
        if($jsonArray === OptConst::JSON_FALSE) {
            $result = ["result"=>CodeConst::Json_Format_Failed,'operation'=>'','data'=>array(), "result_message"=>"JSON格式错误"];
        } else {
            //判断数据格式是否正确
            $result = $this->checkRequestParams($jsonArray);
            if($result === true) {
                return $jsonArray;
            }
        }
        return $result;
    }

    /**
     * 检查推送格式
     * @param $jsonString
     * @return bool|int
     */
    public function checkupServerFormat($jsonString)
    {
        //解析json字符串为数组
        $jsonArray = $this->jsonDecode($jsonString);
        if($jsonArray === OptConst::JSON_FALSE) {  //判断是否为json格式
            $result = OptConst::CORRECT_FORMAT;
        } else {
            $result = OptConst::POSTARRAY_TRUE;
            //判断数据格式是否正确
            if(isset($jsonArray['module']) && $jsonArray['module'] == "GW_Push") {
                if(!isset($jsonArray['type'])) {
                    $result = OptConst::CORRECT_FORMAT;
                }

                if(!isset($jsonArray['object_array'])) {
                    $result = OptConst::CORRECT_FORMAT;
                }

                if(!isset($jsonArray['content'])) {
                    $result = OptConst::CORRECT_FORMAT;
                }
            } else if(isset($jsonArray['module']) && $jsonArray['module'] == "GW_Server") {
                if(!isset($jsonArray['operation'])) {
                    $result = OptConst::CORRECT_FORMAT;
                }
            } else {
                $result = OptConst::CORRECT_FORMAT;
            }


            if($result === OptConst::POSTARRAY_TRUE) {
                return $jsonArray;
            }
        }

        return $result;
    }

    /**
     * 判断数据格式是否正确
     * @param $requestAry
     * @return array|bool
     */
//    public function checkPostParams($requestAry)
//    {
//        if(!isset($requestAry['msgType'])) {
//            Logger::getInstance()->error( "function(checkRequestParams):lack of msgType"." in file".__FILE__." on Line ".__LINE__);
//            return array('result'=>-20,'operation'=>"",'data'=>array("missing_parameter"=>"msgType"),'result_message'=>"缺少参数");
//        }
//        if(!isset($requestAry['content'])) {
//            Logger::getInstance()->error( "function(checkRequestParams):lack of content"." in file".__FILE__." on Line ".__LINE__);
//            return array('result'=>-20,'operation'=>"",'data'=>array("missing_parameter"=>"content"),'result_message'=>"缺少参数");
//        }
//        return OptConst::POSTARRAY_TRUE;
//    }


}